//
//  RZQFindWebViewController.m
//  LastOne
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 sc. All rights reserved.
//

#import "RZQFindWebViewController.h"
#import "FitHeader.h"
#import "HTTPMethod.h"
#import "RZQLikeSave.h"
#import "CoreDataManager.h"
#import "NightHeader.h"
#import "GiFHUD.h"
@interface RZQFindWebViewController ()<UIWebViewDelegate>



@property(nonatomic, strong)UIWebView *webView;
@property(nonatomic, strong)UIImageView *imageZan;
@property(nonatomic, assign)NSInteger zan;
@property(nonatomic, strong)RZQLikeSave *netSave;
@property(nonatomic, strong)CoreDataManager *manger;
@property(nonatomic, strong)NSArray *dataSource;
@property(nonatomic, strong)UILabel *zanLabel;

@end

@implementation RZQFindWebViewController


-(void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    _manger = [[CoreDataManager alloc]init];
    
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, -20 * FitHeight, 375.0 * FitWidth, ScreenHeight - 29 *FitHeight)];
    
    _webView.delegate = self;
    [self.view addSubview:_webView];
     [GiFHUD setGifWithImageName:@"loading.gif"];
    [GiFHUD show];
    
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://app.qdaily.com/app/articles/%ld.html", _modelData.iD]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];
    [_webView NightWithType:UIViewColorTypeNormal];
    _webView.scrollView.bounces = NO;
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view NightWithType:UIViewColorTypeNormal];
    [self.view addSubview:_webView];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(-2 * FitWidth, _webView.frame.size.height - 20 * FitHeight , ScreenWidth +10 * FitWidth, 59 * FitHeight)];
    label.layer.borderWidth = 1.0;
    UIImageView *imageBack = [[UIImageView alloc]initWithFrame:CGRectMake(20 * FitWidth, 10 *FitHeight , 25 * FitWidth, 25 * FitHeight)];
    imageBack.image = [UIImage imageNamed:@"back_Image"];
    imageBack.userInteractionEnabled = YES;
   
    
    label.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapBack = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(backAction)];
    [imageBack addGestureRecognizer:tapBack];
 
    [label addSubview:imageBack];

    _imageZan = [[UIImageView alloc]initWithFrame:CGRectMake( 250 * FitWidth, 10 *FitHeight , 25 * FitWidth, 25 * FitHeight)];
    _imageZan.image = [UIImage imageNamed:@"xihuan"];
    _imageZan.userInteractionEnabled = YES;
    _dataSource = [NSArray array];
    _zanLabel = [[UILabel alloc]initWithFrame:CGRectMake(_imageZan.frame.origin.x + _imageZan.frame.size.width + 10 * FitWidth, _imageZan.frame.origin.y, 40 * FitWidth, _imageZan.frame.size.height)];
    _zanLabel.text = [NSString stringWithFormat:@"%ld", _modelData.praise_count];
    _zanLabel.font = [UIFont systemFontOfSize:18 * FitWidth];
    [label addSubview:_zanLabel];
    _zan = 0;
    
    //判断该网页是否收藏过
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"RZQLikeSave"];
    _dataSource = [_manger.managedObjectContext executeFetchRequest:fetchRequest error:nil];
    for (RZQLikeSave *rzq  in _dataSource) {
        if ([rzq.title isEqualToString:_modelData.title]) {
            _imageZan.image = [UIImage imageNamed:@"shixin"];
             _zanLabel.text = [NSString stringWithFormat:@"%ld", _modelData.praise_count + 1];
            _zan = 1;
        }
    }
    UITapGestureRecognizer *tapLike = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(likeAction)];
    [_imageZan addGestureRecognizer:tapLike];
    [label addSubview:_imageZan];
    
    [self.view addSubview:label];
    UISwipeGestureRecognizer *swipeBack = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(backAction)];
    [self.view addGestureRecognizer:swipeBack];
}

-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)likeAction
{

    if (_zan == 0) {
       self.block(1);
        _imageZan.image = [UIImage imageNamed:@"shixin"];
      _zanLabel.text = [NSString stringWithFormat:@"%ld", _modelData.praise_count + 1];
        _netSave = [NSEntityDescription insertNewObjectForEntityForName:@"RZQLikeSave" inManagedObjectContext:_manger.managedObjectContext];
        _netSave.iD = [NSNumber numberWithInteger:_modelData.iD];
        _netSave.category = [NSKeyedArchiver archivedDataWithRootObject:_modelData.category];
        _netSave.comment_count = [NSNumber numberWithInteger:_modelData.comment_count];
        _netSave.image = _modelData.image;
        _netSave.praise_count = [NSNumber numberWithInteger:_modelData.praise_count];
        _netSave.publish_time = [NSNumber numberWithInteger:_modelData.publish_time];
        
        _netSave.title = _modelData.title;
        [_manger saveContext];
        
        _zan = 1;
        
        
        
    }
    else
    {
          self.block(0);
        _imageZan.image = [UIImage imageNamed:@"xihuan"];
         _zanLabel.text = [NSString stringWithFormat:@"%ld", _modelData.praise_count ];
        NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"RZQLikeSave"];
        for (_netSave in [_manger.managedObjectContext executeFetchRequest:fetchRequest error:nil]) {
            if ([_netSave.title isEqualToString:_modelData.title]) {
                [_manger.managedObjectContext deleteObject:_netSave];
                [_manger saveContext];
                _zan = 0;
            }
            
        }
        
        
        
        
        
        
        
        _zan = 0;
    }
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [GiFHUD dismiss];
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"night"]) {
            [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#2E2E2E'"];
            [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'white'"];
        }
        else{
            [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#FFFFFF'"];
            [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'black'"];
        }
    
    
   
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    [GiFHUD dismiss];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
